﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EF.Model;
using EF.Model.Base;
using EF.Util;
using EF.Util.DBUtility;
using EF.BLL.Base;

public partial class Trans_Base_Function : BasePage
{
    private FunctionService funcBLL = new FunctionService();

    int userId = 0; //用户Id
    string role = ""; //角色
    string lang = ""; //语言

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckSession();

        userId = Convert.ToInt32(Session["uid"].ToString()); //用户Id
        role = Session["role"].ToString(); //角色
        lang = Session["lang"].ToString(); //语言

        Response.Clear();

        switch (Request["action"].ToString().ToLower())
        {
            case "func_points":
                GetPageFunctionPoints(); //根据角色和语言获取页面的功能点
                break;
            case "func_tree":
                GetFunctionTree(); //获取功能点树
                break;
            case "export": //导出
                Export();
                break;
            case "add":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "model":
                GetModel();
                break;
            case "del":
                Delete();
                break;
            default:
                break;
        }

        Response.End();
    }

    private void Add()
    {
        try
        {
            FunctionModel model = (FunctionModel)JsonHelp.Convert2Entity(Request["model"].ToString(), typeof(FunctionModel));
            model.CreateUser = userId;

            string msg;
            int result = funcBLL.Add(model, lang, out msg);
            Response.Write("{success:true,result:" + result + ",msg:'" + msg + "'}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Edit()
    {
        try
        {
            FunctionModel model = (FunctionModel)JsonHelp.Convert2Entity(Request["model"].ToString(), typeof(FunctionModel));
            model.UpdateUser = userId;

            string msg;
            Response.Write("{success:true,result:" + funcBLL.Edit(model, lang, out msg) + ",msg:'" + msg + "'}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetModel()
    {
        try
        {
            int selFuncId = Convert.ToInt32(Request["sel_func_id"].ToString());
            FunctionModel model = funcBLL.GetModel(selFuncId);

            Response.Write("{success:true,rows:" + JsonHelp.Convert2Json(model) + "}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Delete()
    {
        try
        {
            int selFuncId = Convert.ToInt32(Request["sel_func_id"].ToString());

            string msg;
            Response.Write("{success:true,result:" + funcBLL.Delete(selFuncId, lang, out msg) + ",msg:'" + msg + "'}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    /// <summary>
    /// 根据角色和语言获取页面的功能点
    /// </summary>
    private void GetPageFunctionPoints() 
    {
        try
        {
            Response.Write("{success:true,rows:{" + funcBLL.GetPageFunctionPoints(role, Convert.ToInt32(Request["page_id"].ToString()), lang) + "}}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetFunctionTree()
    {
        try
        {
            DataTable dt = funcBLL.GetFunctionTree(lang);

            string treeJson = "[" + JsonHelp.ConvertToTreeJson(dt, "id", "FatherId", 0) + "]";

            treeJson = treeJson.Replace("'*'", "false");

            Response.Write(treeJson);
            
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Export()
    {
        try
        {

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }
}
